﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;
using DataAccess;

namespace BaseWeb
{
    public partial class Tree_Folding : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindMenu();
            }
        }

        /// <summary>
        /// 生成按钮
        /// </summary>
        private void BindMenu()
        {
            List<li_Menu> arryMenu = CommonSystem.Instance.Service.GetListByWhere<li_Menu>(c => c.ParentKeyId == "0").OrderBy(c => c.OrderIndex).ToList();
            foreach (var enMenu in arryMenu)
            {
                //生成主按钮
                System.Web.UI.HtmlControls.HtmlGenericControl TopDiv = new System.Web.UI.HtmlControls.HtmlGenericControl();
                TopDiv.TagName = "div";
                TopDiv.ID = "MainDiv_" + enMenu.KeyId;
                TopDiv.InnerText = TopDiv.InnerHtml = enMenu.Name;
                TopDiv.Attributes.Add("class", "menu");
                divBody.Controls.Add(TopDiv);
                //生成panel
                System.Web.UI.HtmlControls.HtmlGenericControl panelDiv = new System.Web.UI.HtmlControls.HtmlGenericControl();
                panelDiv.TagName = "div";
                panelDiv.ID = "MainPenelDiv_" + enMenu.KeyId;
                panelDiv.Attributes.Add("class", "panel");
                divBody.Controls.Add(panelDiv);
                //在panel里添加链接
                BindNode(panelDiv, enMenu.KeyId);
            }
        }

        /// <summary>
        /// 创建字按钮
        /// </summary> 
        private void BindNode(System.Web.UI.HtmlControls.HtmlGenericControl parentDiv, string parentKeyId)
        {
            List<li_Menu> arrayMenu = CommonSystem.Instance.Service.GetListByWhere<li_Menu>(c => c.ParentKeyId == parentKeyId).OrderBy(c => c.OrderIndex).ToList();
            foreach (var enMenu in arrayMenu)
            {
                var url = enMenu.Url.Replace("#", "");

                //生成按钮
                System.Web.UI.HtmlControls.HtmlGenericControl TopDiv = new System.Web.UI.HtmlControls.HtmlGenericControl();
                TopDiv.TagName = "div";
                TopDiv.InnerText = enMenu.Name; 
                if (string.IsNullOrEmpty(url))
                {
                    TopDiv.ID = "Div_btn_" + enMenu.KeyId; 
                    TopDiv.Attributes.Add("class", "btn");
                }
                else
                {
                    TopDiv.ID = "Div_url_" + enMenu.KeyId;
                    string jstring = string.Format("window.open('{0}','WorkFrame')", enMenu.Url);
                    TopDiv.Attributes.Add("onclick", jstring);
                    TopDiv.Attributes.Add("class", "url"); 
                }
                parentDiv.Controls.Add(TopDiv);
                if (string.IsNullOrEmpty(url))
                { //添加子控件
                    //生成panel
                    System.Web.UI.HtmlControls.HtmlGenericControl panelDiv = new System.Web.UI.HtmlControls.HtmlGenericControl();
                    panelDiv.TagName = "div";
                    panelDiv.ID = "PenelDiv_" + enMenu.KeyId;
                    panelDiv.Attributes.Add("class", "panel");
                    parentDiv.Controls.Add(panelDiv);
                    //在panel里添加子控件
                    BindNode(panelDiv, enMenu.KeyId);
                }


            }
        }







    }
}
